const md5 = require("js-md5")
const {
  USER_NAME_OR_PASS_WORD,
  USER_NAME_IS_COPY
} = require("../contants/error-type")

const {
  login
} = require("../services/auth.services")

const verifUser = async (req, res, next) => {
  const { name, password } = req.body;
  console.log(name, password)
  if (name == "" || password == "" || !name || !password) {
    await next(new Error(USER_NAME_OR_PASS_WORD)); // 中间件拦截
    // return
  }
  await next()


};

// md5 加密
const md5Password = async (req, res, next) => {
  const { password, name } = req.body;

  req.body.password = md5(password);
  // 加密后判断名字有没有与数据库重复
  const result = await login(req.body)
  console.log(result, "===")
  if (result.length !== 0) {
    if (result[0].name == name) {
      return await next(new Error(USER_NAME_IS_COPY))
    }
  }

  await next()

};





module.exports = {
  verifUser,
  md5Password
}